package com.imooc.service.impl;

import com.github.pagehelper.PageHelper;
import com.imooc.base.BaseInfoProperties;
import com.imooc.base.RabbitMqConfig;
import com.imooc.entity.bo.CommentBo;
import com.imooc.entity.mo.MessageMO;
import com.imooc.entity.pojo.Comment;
import com.imooc.entity.pojo.Vlog;
import com.imooc.entity.vo.CommentVo;
import com.imooc.enums.MessageEnum;
import com.imooc.enums.YesOrNo;
import com.imooc.mapper.CommentMapper;
import com.imooc.service.CommentService;
import com.imooc.service.MsgService;
import com.imooc.service.VlogService;
import com.imooc.utils.JsonUtils;
import com.imooc.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.springframework.beans.BeanUtils.copyProperties;

@Service

public class CommentServiceImpl extends BaseInfoProperties implements CommentService {
    @Autowired
    private CommentMapper commentManager;
    @Autowired
    private Sid sid;
    @Autowired
    private MsgService msgService;
    @Autowired
    private VlogService vlogService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public CommentVo createComment(CommentBo commentBo) {
        Comment comment = new Comment();
        comment.setId(sid.nextShort());
        BeanUtils.copyProperties(commentBo,comment);
        comment.setCreateTime(new Date());
        comment.setLikeCounts(0);
        commentManager.createComment(comment);
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment,commentVo);
        redis.increment(REDIS_USER_COMMENT_COUNTS+":"+commentBo.getVlogId(),1);
        // 系统消息: 评论/回复
        Map msgContent = new HashMap();
        Vlog vlog = vlogService.getVlog(commentBo.getVlogId());
        Integer type = MessageEnum.COMMENT_VLOG.type;
        msgContent.put("vlogId",vlog.getId());
        msgContent.put("vlogCover",vlog.getCover());
        msgContent.put("commentId",comment.getId());
        msgContent.put("commentContent",commentBo.getContent());
        if(StringUtils.isNotBlank(commentBo.getFatherCommentId())&&!commentBo.getFatherCommentId().equalsIgnoreCase("0")){
            type = MessageEnum.REPLY_YOU.type;
        }
        // mq 异步解耦
        MessageMO messageMO = new MessageMO();
        messageMO.setFromUserId(commentBo.getCommentUserId());
        messageMO.setToUserId(commentBo.getVlogerId());
        messageMO.setMsgContent(msgContent);
        messageMO.setMsgType(type);

        rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_NAME,"sys.msg."+MessageEnum.COMMENT_VLOG.enValue, JsonUtils.objectToJson(messageMO));
//        msgService.createMsg(commentBo.getCommentUserId(),
//                commentBo.getVlogerId(),type ,msgContent);
        return commentVo;
    }

    @Override
    public PagedGridResult getCommentList(String vlogId, String userId, Integer page, Integer pageSize) {

        PageHelper.startPage(page,pageSize);
        List<CommentVo> commentVos = commentManager.getCommentList(vlogId, userId);
        for (CommentVo commentVo : commentVos) {
            String commentId = commentVo.getCommentId();
            String userLikeThisCounts = redis.get(REDIS_USER_COMMENT_BE_LIKED_COUNTS + ":" + userId + ":" + commentId);
            if(userLikeThisCounts==null){
                commentVo.setLikeCounts(0);
            }else{
                commentVo.setLikeCounts(Integer.valueOf(userLikeThisCounts));
            }
            if(userLikeThisCountYesOrNo(commentId,userId)){
                commentVo.setIsLike(YesOrNo.YES.type);
            }else {
                commentVo.setIsLike(YesOrNo.NO.type);
            }
        }
        return setterPagedGrid(commentVos,page);
    }
    private boolean userLikeThisCountYesOrNo(String commentId,String userId){
        String userBeLike = redis.get(REDIS_USER_BE_LIKE_THIS_COMMENT + ":" + userId + ":" + commentId);
        return userBeLike!=null &&userBeLike.equalsIgnoreCase("1");
    }
    @Override
    public void userBeLikedComment(String userId, String commentId) {
        redis.increment(REDIS_USER_COMMENT_BE_LIKED_COUNTS+":"+userId+":"+commentId,1);
        // 记录用户喜欢该评论
        redis.set(REDIS_USER_BE_LIKE_THIS_COMMENT+":"+userId+":"+commentId,"1");
        Comment comment = commentManager.getCommentById(commentId);
        String vlogId = comment.getVlogId();
        Vlog vlog = vlogService.getVlog(vlogId);
        Map msgContent = new HashMap();
        msgContent.put("commentId",commentId);
        msgContent.put("vlogId",vlogId);
        msgContent.put("vlogCover",vlog.getCover());
       msgService.createMsg(userId,comment.getCommentUserId(),MessageEnum.LIKE_COMMENT.type,msgContent);
    }

    @Override
    public void userBeUnLikedComment(String userId, String commentId) {
        redis.decrement(REDIS_USER_COMMENT_BE_LIKED_COUNTS+":"+userId+":"+commentId,1);
        // 删除用户喜欢该评论的记录
        redis.del(REDIS_USER_BE_LIKE_THIS_COMMENT+":"+userId+":"+commentId);
        Comment commentById = commentManager.getCommentById(commentId);
        String vlogerId = commentById.getVlogerId();
        MessageMO messageMO = new MessageMO();
        messageMO.setFromUserId(userId);
        messageMO.setToUserId(vlogerId);
//        msgService.unLikeThiComment(userId,vlogerId);
        rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_NAME,"sys.msg."+MessageEnum.UN_LIKE_COMMENT.enValue,JsonUtils.objectToJson(messageMO));
    }

    @Override
    public void delThisCount(String commentUserId, String commentId, String vlogId) {
        redis.decrement(REDIS_USER_COMMENT_COUNTS+":"+vlogId,1);

        commentManager.delThisCount(commentUserId,commentId,vlogId);
    }
}
